package org.evelsoft.web.listeners;

import java.util.Properties;
import javax.naming.Context;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * Web application lifecycle listener.
 * @author wojtek
 */
public class StartupContexListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent sce) {

		Properties props_c = new Properties();
		props_c.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
		props_c.setProperty(Context.PROVIDER_URL, "iiop://127.0.0.1:5037:1.2");
		props_c.setProperty("org.omg.CORBA.ORBInitialPort", "5037");
		//Context.P
		Properties props = new Properties();
		//props.setProperty("java.naming.factory.initial","com.sun.enterprise.naming.SerialInitContextFactory");
		//props.setProperty("java.naming.factory.url.pkgs","com.sun.enterprise.naming");
		//props.setProperty("java.naming.factory.state","com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");

		// optional.  Defaults to localhost.  Only needed if web server is running
		// on a different host than the appserver
		//props.setProperty("org.omg.CORBA.ORBInitialHost", "127.0.0.1");

		// optional.  Defaults to 3700.  Only needed if target orb port is not 3700.
		props.setProperty("org.omg.CORBA.ORBInitialPort", "5037");
		//System.getProperties().putAll(props);
		System.out.println("-----------------");
		System.getProperties().getProperty("org.omg.CORBA.ORBInitialPort");
		System.out.println("-----------------");

//		ExpressionFactory ef = ExpressionFactory.newInstance();
		//ef.c
		//props.setProperty(., null)
		//System.getProperties().putAll(props);
		//System.getProperties().put("org.omg.CORBA.ORBInitialPort", "5037");
/*
		try {
			System.out.println("Start WWW :)");
			//InitialContext ic = new InitialContext(props);
			InitialContext ic = new InitialContext(props);
			System.out.println(ic.getEnvironment().toString() + " "+ ic.getNameInNamespace() + " ");
			//TestHelloRemote tr = (TestHelloRemote) ic.lookup(TestHelloRemote.class.getName());//TestHelloRemote.class.getName()
			TestStatelessRemote tr = (TestStatelessRemote) ic.lookup("java:global/pl.edu.agh.onto_onto-ear_ear_1.0-SNAPSHOT/onto-ejb/ejb/ontoStateless!pl.edu.agh.onto.common.interfaces.TestStatelessRemote");//TestStatelessRemote.class.getName()
			System.out.println(tr.testStateless("wojtek on Tomcat"));
		} catch (NamingException ex) {
			ex.printStackTrace();
		}
 * 
 */


		System.out.println("->" + sce.getServletContext().getContextPath());
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		//throw new UnsupportedOperationException("Not supported yet.");
	}
}
